package view.cloudFactory;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import dao.EquipMsgDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.equip.EquipMsgEntity;
import pojo.equip.EquipMsgProperty;
import view.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class RentEquipController {
    private Main myApp;
    private static String  id;
    public void setId(String id) {
        this.id = id;
    }
    private static EquipMsgDao equipMsgDao = new EquipMsgDao();
    private static List<EquipMsgEntity> equipMsgEntities = new ArrayList<>() ;
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public RentEquipController(){}
    @FXML
    private TableView<EquipMsgProperty> equipTable;

    @FXML
    private TableColumn<EquipMsgProperty,Void> option;

    @FXML
    private TableColumn<EquipMsgProperty, String> equipId;

    @FXML
    private TableColumn<EquipMsgProperty, String> code;

    @FXML
    private TableColumn<EquipMsgProperty, String> name;

    @FXML
    private TableColumn<EquipMsgProperty, String> type;

    @FXML
    private TableColumn<EquipMsgProperty, String> mode;

    @FXML
    private TableColumn<EquipMsgProperty, String> detail;

    @FXML
    private TableColumn<EquipMsgProperty, String> equipState;

    @FXML
    private JFXButton rent;

    @ FXML
    void initialize() throws IOException {
        this.displayInfo(equipMsgDao.findUnrentedList());
    }

    @FXML
    void rentEquip(ActionEvent event) throws IOException {
        UserDao userDao = new UserDao();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(equipMsgEntities==null){
            alert.setHeaderText("!");
            alert.setContentText("请选择要租用的设备");
            alert.showAndWait();
        }else{
            for(EquipMsgEntity equip:equipMsgEntities){
                equipMsgDao.changeRentState(equip);
                userDao.addEquip(id,equip);
            }
                alert.setHeaderText("!");
                alert.setContentText("租用成功");
                alert.showAndWait();
        }
        equipMsgEntities = new ArrayList<>() ;
    }
    @FXML
    void returnLastPage(ActionEvent event) throws IOException {
        myApp.goToCloudFactoryManagePage();

    }

    private void addButtonToTable(){
        Callback<TableColumn<EquipMsgProperty, Void>, TableCell<EquipMsgProperty, Void>> cellFactory = new Callback<TableColumn<EquipMsgProperty, Void>, TableCell<EquipMsgProperty, Void>>() {
            @Override
            public TableCell<EquipMsgProperty, Void> call(final TableColumn<EquipMsgProperty, Void> param) {
                final TableCell<EquipMsgProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            EquipMsgProperty equipMsgProperty = getTableView().getItems().get(getIndex());
                            try {
                                equipMsgEntities.add(equipMsgDao.findMsg(equipMsgProperty.getId()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        option.setCellFactory(cellFactory);

    }
    void displayInfo(List<EquipMsgEntity> equipMsgEntityList){
        equipTable.getItems();
        ObservableList<EquipMsgProperty> equipMsgProperties = FXCollections.observableArrayList();
        if(equipMsgEntityList!=null){
            for(EquipMsgEntity equipMsgEntity:equipMsgEntityList){
                EquipMsgProperty equipMsgProperty = new EquipMsgProperty();
                equipMsgProperty.setId(equipMsgEntity.getId());
                equipMsgProperty.setDetail(equipMsgEntity.getDetail());
                equipMsgProperty.setMode(equipMsgEntity.getMode());
                equipMsgProperty.setType(equipMsgEntity.getType());
                equipMsgProperty.setName(equipMsgEntity.getName());
                equipMsgProperty.setCode(equipMsgEntity.getCode());
                equipMsgProperty.setFactory(equipMsgEntity.getFactory());
                equipMsgProperty.setEquipState(equipMsgEntity.getEquipState());
                equipMsgProperty.setSource(equipMsgEntity.getSource());

                equipMsgProperties.add(equipMsgProperty);
            }
            addButtonToTable();

            equipId.setCellValueFactory(param -> param.getValue().idProperty());
            detail.setCellValueFactory(param -> param.getValue().detailProperty());
            mode.setCellValueFactory(param -> param.getValue().modeProperty());
            type.setCellValueFactory(param -> param.getValue().typeProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            code.setCellValueFactory(param -> param.getValue().codeProperty());
            equipState.setCellValueFactory(param -> param.getValue().equipStateProperty());

            equipTable.setItems(equipMsgProperties);
        }else{
            equipTable.setItems(FXCollections.emptyObservableList());
        }

    }

}
